package com.sz.common.base.service.impl;

import com.sz.common.base.dao.DaoSupport;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.I18nBaseDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.entity.I18nEntity;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.service.I18nBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Function: 通用国际化接口 <br>
 * Author: hpeng.wang <br>
 * Date: 2016-12-01 16:30:00
 */
@Service("i18nBaseService")
public class I18nBaseServiceImpl extends AbstractService implements I18nBaseService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Override
    protected String getMapperNamespace() {
        return "I18nMapper";
    }

    @Override
    public void save(Serializable refId, List<? extends I18nBaseDto> dtoList, String tableName) {
        //1、将dtoList转成I18nEntityList集合
        List<I18nEntity> i18nEntityList = I18nBaseDto.getI18nEntry(dtoList, tableName, refId);
        //2、删除数据库已经存在的当前refId有关的的数据
        deleteByRefId(tableName, refId);
        if (i18nEntityList != null && i18nEntityList.size() > 0) {
            //3、去除不需要保存的空数据
            List<I18nEntity> removeList = new ArrayList<>();
            for (I18nEntity entity : i18nEntityList) {
                if (!entity.hasI18nValue()) {//所有的国际化字段全部为null或""则不进行保存记录
                    removeList.add(entity);
                }
            }
            i18nEntityList.removeAll(removeList);//将不需要保存的数据从集合中删除
            //4、批量保存剩余的数据
            dao.batchInsert(getSqlName("insert"), i18nEntityList);
        }
    }

    @Override
    public void save(List<? extends I18nBaseDto> dtoList, String tableName) {
        //1、将dtoList转成I18nEntityList集合
        List<I18nEntity> i18nEntityList = I18nBaseDto.getI18nEntry(dtoList, tableName, null);
        //2、拼装要处理的集合
        List<I18nEntity> removeList = new ArrayList<>();
        List<I18nEntity> updateList = new ArrayList<>();
        List<I18nEntity> saveList = new ArrayList<>();
        if (i18nEntityList != null && i18nEntityList.size() > 0) {
            //对不需要保存的空数据删除
            for (I18nEntity entity : i18nEntityList) {
                if (!entity.hasI18nValue()) {//没有数据，删除
                    removeList.add(entity);
                } else if (isExist(entity)) {//数据库已经存在，更新
                    updateList.add(entity);
                } else {                     //数据库不存在，添加
                    saveList.add(entity);
                }
            }
            //3、对相应的集合做处理
            dao.batchInsert(getSqlName("insert"), saveList);
            dao.batchDelete(getSqlName("deleteByRefIdAndLanguage"), removeList);
            dao.batchUpdate(getSqlName("update"), updateList);
        }
    }

    @Override
    public void deleteByRefId(String tableName, Serializable refId) {
        ParamData pd = new ParamData();
        pd.put("tableName", tableName);
        pd.put("refId", refId);
        dao.delete(getSqlName("deleteByRefId"), pd);
    }

    @Override
    public void deleteByRefIds(String tableName, List refIds) {
        ParamData pd = new ParamData();
        pd.put("tableName", tableName);
        pd.put("refIds", refIds);
        dao.delete(getSqlName("deleteByRefIds"), pd);
    }

    @Override
    public List<Map> findByRefId(String tableName, Serializable refId) {
        ParamData pd = new ParamData();
        pd.put("tableName", tableName);
        pd.put("refId", refId);
        return dao.findForList(getSqlName("findByRefId"), pd, Map.class);
    }

    @Override
    public List<I18nDto> findI18nDtoByRefId(String tableName, Serializable refId) {
        List<Map> list = findByRefId(tableName, refId);
        List<I18nDto> i18nDtoList = new ArrayList<>();
        if (list.size() > 0) { //list 永远不会为NULL
            for (Map map : list) {
                I18nDto i18nDto = new I18nDto();
                i18nDto.setLocalName((String) map.get("localName"));
                i18nDto.setLanguage((String) map.get("language"));
                i18nDto.setRefId(map.get("refId"));
                i18nDtoList.add(i18nDto);
            }
        }
        return i18nDtoList;
    }

    private boolean isExist(I18nEntity entity) {
        Integer count = (Integer) this.dao.findForObject(getSqlName("isExist"), entity);
        return count != null && count > 0;
    }
}
